package purchasePlan

import (
	"context"
	"errors"
	"github.com/google/uuid"
	"github.com/shopspring/decimal"
	"insighttracking/apps/purchase/auth"
	"insighttracking/common/enum"
	"insighttracking/query/dao"
	"net/http"

	"insighttracking/apps/purchase/internal/svc"
	"insighttracking/apps/purchase/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
	"os"
)

type CompletePurchasePlanApiLogic struct {
	logx.Logger
	ctx     context.Context
	svcCtx  *svc.ServiceContext
	Request *http.Request
}

func NewCompletePurchasePlanApiLogic(ctx context.Context, svcCtx *svc.ServiceContext, request *http.Request) *CompletePurchasePlanApiLogic {
	hostname, _ := os.Hostname()
	return &CompletePurchasePlanApiLogic{
		Logger: logx.WithContext(ctx).WithCallerSkip(0).WithFields(
			logx.Field("Log_UUID", uuid.New().String()),
			logx.Field("hostname", hostname)),
		ctx:     ctx,
		svcCtx:  svcCtx,
		Request: request,
	}
}

func (l *CompletePurchasePlanApiLogic) CompletePurchasePlanApi(req *types.CompletePurchasePlanReq) (resp string, err error) {
	accountInfo, err := auth.GetAccountInfo(l.svcCtx, l.Request)
	if err != nil {
		return "", err
	}
	tx := dao.Use(l.svcCtx.MysqlDb).Begin()
	if err = tx.Commit(); err != nil {
		return "", err
	}
	for _, item := range req.DataList {
		_, err = dao.PurchasePlanEntity.WithContext(l.ctx).Where(
			dao.PurchasePlanEntity.EnterpriseID.Eq(accountInfo.EnterpriseId),
			dao.PurchasePlanEntity.ID.Eq(item.Id)).First()
		if err != nil {
			if err = tx.Rollback(); err != nil {
				return "", err
			}
			return "", errors.New("传递的采购计划id错误")
		}
		purchaserPrice, _ := decimal.NewFromString(item.PurchaserPrice)
		purchaserCount, _ := decimal.NewFromString(item.PurchaserCount)
		purchaserAmount, _ := decimal.NewFromString(item.PurchaserAmount)
		if _, err = dao.PurchasePlanEntity.WithContext(l.ctx).Where(dao.PurchasePlanEntity.ID.Eq(item.Id)).UpdateSimple(
			dao.PurchasePlanEntity.PurchaserID.Value(item.PurchaserID),          // 采购员id
			dao.PurchasePlanEntity.SupplierID.Value(item.SupplierID),            // 供应商,关联到supplier表主键id
			dao.PurchasePlanEntity.PurchaserPrice.Value(purchaserPrice),         // 采购单价
			dao.PurchasePlanEntity.PurchaserCount.Value(purchaserCount),         // 采购数量
			dao.PurchasePlanEntity.PurchaserAmount.Value(purchaserAmount),       // 采购金额
			dao.PurchasePlanEntity.Status.Value(enum.PurchasePlanCompletedEnum), // 采购状态
		); err != nil {
			if err = tx.Rollback(); err != nil {
				return "", err
			}
			return "", errors.New("操作失败")
		}
	}
	return "操作成功", nil
}

func (l *CompletePurchasePlanApiLogic) ResetPurchasePlanApi(req *types.ResetPurchasePlanReq) (resp string, err error) {
	accountInfo, err := auth.GetAccountInfo(l.svcCtx, l.Request)
	if err != nil {
		return "", err
	}
	tx := dao.Use(l.svcCtx.MysqlDb).Begin()
	if err = tx.Commit(); err != nil {
		return "", err
	}
	for _, item := range req.DataList {
		_, err = dao.PurchasePlanEntity.WithContext(l.ctx).Where(
			dao.PurchasePlanEntity.EnterpriseID.Eq(accountInfo.EnterpriseId),
			dao.PurchasePlanEntity.ID.Eq(item.Id)).First()
		if err != nil {
			if err = tx.Rollback(); err != nil {
				return "", err
			}
			return "", errors.New("传递的采购计划id错误")
		}
		purchaserPrice, _ := decimal.NewFromString("0.00")
		purchaserCount, _ := decimal.NewFromString("0.00")
		purchaserAmount, _ := decimal.NewFromString("0.00")
		if _, err = dao.PurchasePlanEntity.WithContext(l.ctx).Where(dao.PurchasePlanEntity.ID.Eq(item.Id)).UpdateSimple(
			dao.PurchasePlanEntity.PurchaserPrice.Value(purchaserPrice),      // 采购单价
			dao.PurchasePlanEntity.PurchaserCount.Value(purchaserCount),      // 采购数量
			dao.PurchasePlanEntity.PurchaserAmount.Value(purchaserAmount),    // 采购金额
			dao.PurchasePlanEntity.Status.Value(enum.PurchasePlanCreateEnum), // 采购状态
		); err != nil {
			if err = tx.Rollback(); err != nil {
				return "", err
			}
			return "", errors.New("操作失败")
		}
	}
	return "操作成功", nil
}
